<!--
 * @Description: 票务机票订单数据统计
 * @Version: 2.0
 * @Autor: 许国辉
 * @Date: 2022-11-27 14:09:03
 * @LastEditors: 许国辉
 * @LastEditTime: 2022-11-27 14:15:08
-->
<template>
  <PageWrapper>
    <a-tabs v-model:activeKey="activeKey" @change="tabChange">
      <a-tab-pane key="1" tab="有审批单" />
      <a-tab-pane key="2" tab="无审批单" />
    </a-tabs>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="customHeader"> 导出表格 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <!-- 航空公司 -->
        <template v-if="column.key === 'carrierName'">
          {{ record.carrierName + '(' + record.carrierCode + ')' }}
        </template>
        <!-- 改签费 -->
        <template v-if="column.key === 'changeUpFeechangeFee'">
          {{ record.changeUpFee + record.changeFee }}
        </template>
        <!-- 舱位 -->
        <template v-if="column.key === 'cabinCode'">
          {{ record.cabinDesc + '(' + record.cabinCode + ')' }}
        </template>
        <!-- 机票价salePrice -->
        <template v-if="column.key === 'salePrice'">
          <span v-if="record.bookingType === '改期单'">0</span>
          <span v-else>{{ record.salePrice }}</span>
        </template>
        <!-- 税（taxSum） -->
        <template v-if="column.key === 'taxSum'">
          <span v-if="record.bookingType === '改期单'">0</span>
          <span v-else>{{ record.taxSum }}</span>
        </template>
        <!-- 机票合计金额（planePriceSum）  -->
        <template v-if="column.key === 'planePriceSum'">
          <span v-if="record.bookingType === '改期单'">0</span>
          <span v-else>{{ record.planePriceSum }}</span>
        </template>
        <!-- 保险（insurance） -->
        <template v-if="column.key === 'insurance'">
          <span v-if="record.bookingType === '退票单'">-{{ record.insurance }} </span>
          <span v-else>{{ record.insurance }}</span>
        </template>
        <!-- 折扣率（agio） -->
        <template v-if="column.key === 'agio'">
          <span v-if="record.bookingType === '改期单'">0</span>
          <span v-else>{{ record.agio }}</span>
        </template>
        <!-- 服务费（ticketServicePrice）  -->
        <template v-if="column.key === 'ticketServicePrice'">
          <span v-if="record.bookingType === '改期单'">0</span>
          <span v-else>{{ record.ticketServicePrice }}</span>
        </template>
        <!-- 深白云扣款priceBy -->
        <template v-if="column.key === 'priceBy'">
          <span v-if="record.bookingType === '改期单'">{{
            record.changeUpFee + record.changeFee
          }}</span>
          <span v-else-if="record.bookingType === '退票单'">{{
            record.refundPrice + record.priceBy - record.insurance
          }}</span>
          <span v-else>{{ record.priceBy + record.insurance }}</span>
        </template>
        <!-- 应收ticketPrice -->
        <template v-if="column.key === 'ticketPrice'">
          <span v-if="record.bookingType === '改期单'">{{
            record.changeUpFee + record.changeFee
          }}</span>
          <span v-else-if="record.bookingType === '退票单'">{{
            record.refundPrice + record.ticketPrice - record.insurance
          }}</span>
          <span v-else>{{ record.ticketPrice + record.insurance }}</span>
        </template>
        <!-- 总金额包含服务费ticketPriceSum -->
        <template v-if="column.key === 'ticketPriceSum'">
          <span v-if="record.bookingType === '改期单'">{{
            record.changeUpFee + record.changeFee
          }}</span>
          <span v-else-if="record.bookingType === '退票单'">{{
            record.refundPrice + record.ticketPriceSum - record.insurance
          }}</span>
          <span v-else>{{ record.ticketPriceSum + record.insurance }}</span>
        </template>
        <!-- 特别说明isOutLimit -->
        <template v-if="column.key === 'isOutLimit'">
          <div
            v-if="
              record.isOutLimit === '1' &&
              record.bookingType !== '改期单' &&
              record.bookingType !== '退票单'
            "
            style="background-color: #5182ed"
            >{{ record.outLimitReason }}</div
          >
          <div v-else>{{
            record.isOutLimit === 1 ? '超标' + ' ' + record.outLimitReason : '未超标'
          }}</div>
        </template>
      </template>
    </BasicTable>
  </PageWrapper>
</template>
<script lang="ts">
  import { Tabs } from 'ant-design-vue';
  import { defineComponent, onMounted, ref, toRaw } from 'vue';
  import { PageWrapper } from '/@/components/Page';
  import { BasicTable, useTable } from '/@/components/Table';
  import { columns, searchFormSchema } from './orderStatisticsAirData';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import {
    getOrderStatisticsAirBybookingWay1,
    getOrderStatisticsAirBybookingWay2,
  } from '/@/api/orderStaiistics/air/orderStaiisticsAPI';
  import { OrderStatisticsAirModel } from '/@/api/orderStaiistics/air/OrderStatisticsAirModel';
  export default defineComponent({
    components: {
      BasicTable,
      PageWrapper,
      [Tabs.name]: Tabs,
      [Tabs.TabPane.name]: Tabs.TabPane,
    },
    setup() {
      let api = ref(getOrderStatisticsAirBybookingWay1);
      const [registerTable, { getForm, reload, getDataSource }] = useTable({
        api,
        pagination: { pageSize: 10 },
        columns: columns,
        showIndexColumn: true,
        formConfig: {
          labelWidth: 100,
          actionColOptions: { span: 24 },
          schemas: searchFormSchema,
          fieldMapToTime: [['outTicketDate', ['ticketDateStart', 'ticketDateEnd'], 'YYYY-MM-DD']],
        },
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        striped: false,
        useSearchForm: true,
        bordered: true,
      });

      async function tabChange(activeKey) {
        if (activeKey === '1') {
          api.value = getOrderStatisticsAirBybookingWay1;
          await getForm().submit;
          reload();
        } else if (activeKey === '2') {
          api.value = getOrderStatisticsAirBybookingWay2;
          await getForm().submit;
          reload();
        }
      }
      function customHeader() {
        let header = {
          index: '序号',
          linkman: '终端类型',
          outTicketDate: '出票日期',
          bookerName: '预定人姓名',
          bookerTel: '预定人手机',
          exNum: '审批单号',
          exDepartureDate: '审批出发日期',
          exReturnDate: '审批返回日期',
          empNo: '员工编号',
          pnr: 'PNR',
          cabinAllPrice: '全价',
          tickerNo: '票号',
          empName: '员工姓名',
          empTel: '员工电话',
          voyage: '行程',
          flightNum: '航班号',
          carrierName: '航空公司',
          deptDate: '出发日期',
          deptTime: '出发时间',
          arriveDate: '抵达日期',
          arriveTime: '抵达时间',
          cabinCode: '舱位',
          salePrice: '机票价',
          taxSum: '税（总）',
          planePriceSum: '机票合计金额',
          changeFee: '改期费',
          changeUpFee: '升舱费',
          changeUpFeechangeFee: '改签费',
          refundBeAmount: '应退款(含税)',
          insurance: '保险',
          agio: '折扣率',
          ticketServicePrice: '服务费',
          priceBy: '深白云扣款',
          refundPrice: '退票费(财务)',
          ticketPrice: '应收',
          ticketPriceSum: '总金额包含服务费',
          isSettlement: '是否结算',
          isAccount: '白云对帐',
          payDate: '支付时间',
          bookingType: '订票类型',
          belongType: '机票类型',
          remark: '备注',
          empIdNum: '员工身份证号',
          isOutLimit: '特别说明',
        };

        let tableData: OrderStatisticsAirModel[] = toRaw(getDataSource());

        let excalDateArr: any = [];
        for (let i = 0; i < tableData.length; i++) {
          let excalDate = {
            index: 0,
            linkman: '终端类型',
            outTicketDate: '出票日期',
            bookerName: '预定人姓名',
            bookerTel: '预定人手机',
            exNum: '审批单号',
            exDepartureDate: '审批出发日期',
            exReturnDate: '审批返回日期',
            empNo: '员工编号',
            pnr: 'PNR',
            cabinAllPrice: 0,
            tickerNo: '票号',
            empName: '员工姓名',
            empTel: '员工电话',
            voyage: '行程',
            flightNum: '航班号',
            carrierName: '航空公司',
            deptDate: '出发日期',
            deptTime: '出发时间',
            arriveDate: '抵达日期',
            arriveTime: '抵达时间',
            cabinCode: '舱位',
            salePrice: 0,
            taxSum: 0,
            planePriceSum: 0,
            changeFee: 0,
            changeUpFee: 0,
            changeUpFeechangeFee: 0,
            refundBeAmount: 0,
            insurance: 0,
            agio: 0,
            ticketServicePrice: 0,
            priceBy: 0,
            refundPrice: 0,
            ticketPrice: 0,
            ticketPriceSum: 0,
            isSettlement: '是否结算',
            isAccount: '白云对帐',
            payDate: '支付时间',
            bookingType: '订票类型',
            belongType: '机票类型',
            remark: '备注',
            empIdNum: '员工身份证号',
            isOutLimit: '特别说明',
          };

          excalDate.index = i + 1;
          if (tableData[i].linkman === '票务') {
            excalDate.linkman = 'PC';
          } else {
            excalDate.linkman = 'APP';
          }
          excalDate.outTicketDate = tableData[i].outTicketDate;
          excalDate.bookerName = tableData[i].bookerName;
          excalDate.bookerTel = tableData[i].bookerTel;
          excalDate.exNum = tableData[i].exNum;
          excalDate.exDepartureDate = tableData[i].exDepartureDate;
          excalDate.exReturnDate = tableData[i].exReturnDate;
          excalDate.empNo = tableData[i].empNo;
          excalDate.pnr = tableData[i].pnr;
          excalDate.cabinAllPrice = tableData[i].cabinAllPrice;
          excalDate.tickerNo = tableData[i].tickerNo;
          excalDate.empName = tableData[i].empName;
          excalDate.empTel = tableData[i].empTel;
          excalDate.voyage = tableData[i].voyage;
          excalDate.flightNum = tableData[i].flightNum;
          excalDate.carrierName = tableData[i].carrierName + '(' + tableData[i].carrierCode + ')';
          excalDate.deptDate = tableData[i].deptDate;
          excalDate.deptTime = tableData[i].deptTime;
          excalDate.arriveDate = tableData[i].arriveDate;
          excalDate.arriveTime = tableData[i].arriveTime;
          excalDate.cabinCode = tableData[i].cabinDesc + '(' + tableData[i].cabinCode + ')';
          if (tableData[i].bookingType === '改期单') {
            excalDate.salePrice = 0;
            excalDate.taxSum = 0;
            excalDate.planePriceSum = 0;
            excalDate.agio = 0;
            excalDate.ticketServicePrice = 0;
          } else {
            excalDate.salePrice = tableData[i].salePrice;
            excalDate.taxSum = tableData[i].taxSum;
            excalDate.planePriceSum = tableData[i].planePriceSum;
            excalDate.agio = tableData[i].agio;
            excalDate.ticketServicePrice = tableData[i].ticketServicePrice;
          }
          excalDate.changeFee = tableData[i].changeFee;
          excalDate.changeUpFee = tableData[i].changeUpFee;
          excalDate.changeUpFeechangeFee = tableData[i].changeFee + tableData[i].changeUpFee;
          excalDate.refundBeAmount = tableData[i].refundBeAmount;
          if (tableData[i].bookingType === '退票单') {
            excalDate.insurance = -tableData[i].insurance;
          } else {
            excalDate.insurance = tableData[i].insurance;
          }
          excalDate.refundPrice = tableData[i].refundPrice;

          if (tableData[i].bookingType === '改期单') {
            excalDate.priceBy = tableData[i].changeUpFee + tableData[i].changeFee;
            excalDate.ticketPrice = tableData[i].changeFee + tableData[i].changeUpFee;
            excalDate.ticketPriceSum = tableData[i].changeFee + tableData[i].changeUpFee;
          } else if (tableData[i].bookingType === '退票单') {
            excalDate.priceBy =
              tableData[i].refundPrice + tableData[i].priceBy - tableData[i].insurance;
            excalDate.ticketPrice =
              tableData[i].refundPrice + tableData[i].ticketPrice - tableData[i].insurance;
            excalDate.ticketPriceSum =
              tableData[i].refundPrice + tableData[i].ticketPriceSum - tableData[i].insurance;
          } else {
            excalDate.priceBy = tableData[i].priceBy + tableData[i].insurance;
            excalDate.ticketPrice = tableData[i].ticketPrice + tableData[i].insurance;
            excalDate.ticketPriceSum = tableData[i].ticketPriceSum + tableData[i].insurance;
          }
          if (tableData[i].isSettlement === 0) {
            excalDate.isSettlement = '未结算';
          } else {
            excalDate.isSettlement = '已结算';
          }
          if (tableData[i].isAccount === 0) {
            excalDate.isAccount = '未对帐';
          } else {
            excalDate.isAccount = '已对帐';
          }
          excalDate.payDate = tableData[i].payDate;
          excalDate.bookingType = tableData[i].bookingType;
          excalDate.belongType = tableData[i].belongType;
          excalDate.remark = tableData[i].remark;
          excalDate.empIdNum = tableData[i].empIdNum;
          if (
            tableData[i].isOutLimit === 1 &&
            tableData[i].bookingType !== '改期单' &&
            tableData[i].bookingType !== '退票单'
          ) {
            excalDate.isOutLimit = tableData[i].outLimitReason;
          } else {
            excalDate.isOutLimit =
              tableData[i].isOutLimit === 1 ? '超标' + ' ' + tableData[i].outLimitReason : '未超标';
          }
          excalDateArr.push(excalDate);
        }
        jsonToSheetXlsx({
          data: excalDateArr,
          header,
          filename: '机票订单数据统计.xlsx',
        });
      }

      onMounted(() => {});

      return {
        activeKey: ref('1'),
        registerTable,
        tabChange,
        customHeader,
      };
    },
  });
</script>
